﻿using System;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.UI.UserControls;

namespace DNNToolkit.Modules.CityTaxLocator
{
    /// <summary>
    /// The Settings class manages Module Settings.
    /// </summary>
    public partial class Settings : ModuleSettingsBase
    {

        #region Base Method Implementations

        /// <summary>
        /// LoadSettings loads the settings from the Database and displays them.
        /// </summary>
        public override void LoadSettings()
        {
            try
            {
                if (Page.IsPostBack == false)
                {
                    if (TabModuleSettings["headertemplate"] != null)
                    {
                        txtHeader.Text = TabModuleSettings["headertemplate"].ToString();
                    }

                    if (TabModuleSettings["footertemplate"] != null)
                    {
                        txtFooter.Text = TabModuleSettings["footertemplate"].ToString();
                    }

                }
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        /// <summary>
        /// UpdateSettings saves the modified settings to the Database.
        /// </summary>
        public override void UpdateSettings()
        {
            try
            {
                ModuleController objModules = new ModuleController();
                objModules.UpdateTabModuleSetting(TabModuleId, "headertemplate", txtHeader.Text);
                objModules.UpdateTabModuleSetting(TabModuleId, "footertemplate", txtFooter.Text);

                //refresh cache
                ModuleController.SynchronizeModule(this.ModuleId);
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion


        protected TextEditor txtHeader
        {
            get
            {
                return (TextEditor)this.txtHeaderTemplate;
            }
            set
            {
                this.txtHeaderTemplate = value;
            }
        }

        protected TextEditor txtFooter
        {
            get
            {
                return (TextEditor)this.txtFooterTemplate;
            }
            set
            {
                this.txtFooterTemplate = value;
            }
        }

    }
}